
//
// 120205 - AH - (Bien) Created.
// 120321 - AH - Removed @Override for interface method (1.5 compatible).
//

package org.soekrat.gui.swing.motor.passingstrategy;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.motor.passingstrategies.PassingStrategy;
import org.soekrat.gui.swing.motor.wheelpassingstrategy.WheelPassingStrategyPanel;

/**
 * <p>
 *   Panel for choosing which 
 *   {@link PassingStrategy} to use and specify how the one selected is to be
 *   used.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class PassingStrategyPanel extends JPanel 
{
  private final WheelPassingStrategyPanel m_wpsp;
  
  private final JCheckBox m_useWheelPsp=new JCheckBox();
  
  private final CheckForContentPassingStrategyPanel m_cfcpsp;
  
  private final JCheckBox m_useCheckPsp=new JCheckBox();
  
  private final OntologySequenceDefiningPanel m_osdp;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param se {@link Soekrat2Engine} interfaced.
   */
  public PassingStrategyPanel(Soekrat2Engine se)
  {
    if (se==null)
    {
      throw new NullPointerException("se");  
    }
    
    setLayout(new BorderLayout());
    
    ButtonGroup bg=new ButtonGroup();
    bg.add(m_useCheckPsp);
    bg.add(m_useWheelPsp);
    
    m_osdp=new OntologySequenceDefiningPanel(se);
    
    m_cfcpsp=new CheckForContentPassingStrategyPanel(se);
    m_wpsp=new WheelPassingStrategyPanel(se);
    
    Box center=new Box(BoxLayout.Y_AXIS);
    
    Box row=new Box(BoxLayout.X_AXIS);
    m_useWheelPsp.addActionListener(m_selectAction);
    row.add(m_useWheelPsp);
    row.add(m_wpsp);
    center.add(row);
    
    row=new Box(BoxLayout.X_AXIS);
    m_useCheckPsp.addActionListener(m_selectAction);
    row.add(m_useCheckPsp);
    row.add(m_cfcpsp);
    center.add(row);
    
    add(center, BorderLayout.CENTER);
    
    add(m_osdp, BorderLayout.SOUTH);
    
    m_useWheelPsp.setSelected(true);
    m_useCheckPsp.setSelected(false);
    
    m_cfcpsp.setEnabled(false);
  }
  
  private ActionListener m_selectAction=new ActionListener()
  {
    public void actionPerformed(ActionEvent e)
    {
      m_osdp.setEnabled(false);
      m_wpsp.setEnabled(false);
      m_cfcpsp.setEnabled(false);
      
      JCheckBox source=(JCheckBox)e.getSource();
      if (source.isSelected())
      {
        m_osdp.setEnabled(true);
        
        if (source==m_useWheelPsp)
        {
          m_wpsp.setEnabled(true);
          m_osdp.setSequenceReceiver(m_wpsp);
        }
        else if (source==m_useCheckPsp)
        {
          m_cfcpsp.setEnabled(true);
          m_osdp.setSequenceReceiver(m_cfcpsp);
        }
      }
    }
  };
  
}
